package cn.tedu.api;

import org.junit.Test;

/*本类用于字符串拼接 StringBuffer线程安全*/
public class TestString {
    @Test
    public void test(){
        String s="abcdefghijklmnopqrstuvwxyz";

   /* method(s);*/
        /*StringBuffer*/
    method1(s);

    }
    private void method1(String s) {

        /*优化一 使用StringBuilder StringBuffer*/
        StringBuilder builder=new StringBuilder();
        StringBuffer buffer = new StringBuffer();
        long l = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            /*优化2 不再使用+拼接，而是使用append()拼接*/
            buffer.append(s);
            builder.append(s);
        }
        long l1 = System.currentTimeMillis();
        System.out.println("花费时间为"+(l1-l)+"ms");
    }

    private void method(String s) {
        String result="";
        long l = System.currentTimeMillis();
        for (int i = 0; i <10000 ; i++) {
            result+=s;
        }
        long l1 = System.currentTimeMillis();
        System.out.println("花费时间为"+(l1-l)+"ms");
        //  System.out.println(result);
    }

}
